package com.lsl.lambdatestTest;

import com.sun.imageio.plugins.common.I18N;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Properties;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;

import static java.lang.System.out;

/**
 * @author lishuailong
 * @create 2020-07-17 14:40
 */
public class test1 {
    @Test
    public void test1() {
        Runnable runnable = () -> out.println("123");
        runnable.run();

        //方式1
        Consumer<String> consumer = (String s) -> {
            out.println(s);
        };
        consumer.accept("consumer");


        //方式2
        Consumer<String> consumer1 = (s) -> {
            out.println(s);
        };
        consumer1.accept("consumer1");

        //方式3
        Consumer<String> consumer2 = (s) -> {
            out.println(s);
        };
        consumer2.accept("consumer1");
    }

    @Test
    public void test2() {
        Comparator<Integer> comparator = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                out.println("o1 = " + o1);
                out.println("o2 = " + o2);
                return o1.compareTo(o2);
            }
        };

        Comparator<Integer> comparator1 = (o1, o2) -> {
            out.println("o1 = " + o1);
            out.println("o2 = " + o2);
            return o1.compareTo(o2);
        };


    }

    ;

    @Test
    public void test3() {
        happlTime(2, new Consumer<Double>() {
            @Override
            public void accept(Double aDouble) {
                out.println(aDouble + "元一碗！");
            }
        });

        happlTime(20, s -> {
            out.println(s + "元一碗！");
            out.println(s + "元一碗！");
        });


    }

    ;

    public void happlTime(double d, Consumer<Double> consumer) {
        consumer.accept(d);
    }

    @Test
    public void test4() {
        List<String> list = Arrays.asList(new String[]{"11","233","34","45"});
        List<String> list1 = filterString(list, new Predicate<String>() {
            @Override
            public boolean test(String s) {
                if (s.length() == 2) {
                    return true;
                }
                return false;
            }
        });
        out.println("list1 = " + list1);

        List<String> list2 = filterString(list, s -> {return s.length() == 2;});
        out.println("list2 = " + list2);
        list.forEach((String s) -> {
            out.println(s);
        });
    }

    //根据给定的规则过来集合
    public List<String> filterString(List<String> list, Predicate<String> pre) {
        List<String> listz = new ArrayList<>();
        for (String str : list) {
            if (pre.test(str)) {
                listz.add(str);
            }
        }
        return listz;
    }

    @Test
    public void test5() {
        Runnable runnable = () -> {
            out.println("123");
        };
    }
    @Test
    public void test6() {
        Supplier<String> stringSupplier = () -> "ain";
        String s = stringSupplier.get();
        out.println("s = " + s);
        String s1 = "666";

        Supplier<String> stringSupplier1 = s1::toString;
        out.println(stringSupplier1);
    }
    @Test
    public void test7() {
        Comparator<Integer> comparator = (i1,i2) -> Integer.compare(i1,i2);
        Comparator<Integer> comparator1 = Integer::compare;
    }

}
